import { shareToFriend } from "@/api/share.js";
import util from "./util";
import cache from "@/utils/cache";
class Share {
  constructor() {
    this.url = "";
    this.opt = {};
	this.menuArr = []
  }

    getShareConfig(opt, menuArr, hideArr, companyName) {
        // let pages = location.href
        this.url = location.hash.split('#/')[1].split('?')[0]
        this.opt = opt;
        this.menuArr = menuArr;
        this.hideArr = hideArr
        this.companyName = companyName
        // this.getShareConfigSession()
        this.getTicket()
  }

    getTicket() {
        const strMap = {
            "H5-dev": "https://dev-manage.julimeng.com/2h5/",
            "H5-test": "https://test-manage.julimeng.com/2h5/",
            "H5-uat": "https://dev-manage.julimeng.com/2h5/",
            "H5-prod": "https://manage.shengdaiqifu.com/2h5/",
        };
        let params = { url: strMap[process.env.ENV_TYPE] };
        shareToFriend(params).then((res) => {
            this.setShareConfig(res.data)
        });
    }
    setShareConfig(shareConfig) {
        const str = util.getPageUrl(this.opt, this.url);
        if (str) {
            util.setShare(shareConfig, str, this.menuArr, this.hideArr, this.companyName);
        }
    }
    getShareConfigSession() {
      let signShare = cache.get('ticket');
      if (signShare) {
          let share = JSON.parse(signShare)
          if (this.isSignValid(share)) { //判断签名是否过期
              this.getTicket() //签名过期，重新获取
          } else {
              this.setShareConfig(share) //没过期获取session中存的签名令牌
          }
      } else {
          this.getTicket() //签名不存在，直接获取
      }
    }
    isSignValid(ticket) {
      let now = new Date().getTime();
      let newDate = Number(ticket.timestamp) + 7200 * 1000;
      return now < newDate;
    }
}

export default new Share();
